local yujian = fk.CreateSkill{
    name = "wk_heg__yujian",
}

Fk:loadTranslationTable{
    ["wk_heg__yujian"] = "御谏",
    [":wk_heg__yujian"] = "其他角色于其出牌阶段内首次对与你势力相同的角色造成伤害后，你可依次执行：1.若其体力值不大于你，你可与其交换手牌，且此回合结束时你与其交换手牌；"..
    "2.若其武将牌均明置，你可暗置其一张武将牌且直至本回合结束不能明置之。",

    ["wk_heg__yujian_hide"] = "御谏：暗置%dest一张武将牌且其本回合内不能明置",

    ["@@wk_heg__yujian_exchange-turn"] = "御谏 交换手牌",
    ["@wk_heg__yujian_reveal-turn"] = "御谏 禁亮",

    ["$wk_heg__yujian1"] = "臣代天子牧民，闻苛自当谏之。",
    ["$wk_heg__yujian2"] = "为将者死战，为臣者死谏。",
}

local H = require "packages.ol_hegemony.util"

yujian:addEffect(fk.Damage,{
    anim_type = "control",
    can_trigger = function (self, event, target, player, data)
       if not (player:hasSkill(yujian.name) and target ~= player and data.from.phase == Player.Play and H.compareKingdomWith(player, data.to)) then return false end
       local events = player.room.logic:getActualDamageEvents(2, function(e)
        local use = e.data
        return H.compareKingdomWith(use.to, player) and use.from == player.room.current and use.from.phase == Player.Play
        end, Player.HistoryTurn)
       return #events == 1
    end,
    on_use = function (self, event, target, player, data)
       local room = player.room
       local current = room.current
       if target.hp <= player.hp then
        room:swapAllCards(player, {player, current}, yujian.name, "h")
        room:setPlayerMark(player, "@@wk_heg__yujian_exchange-turn", 1)
       end
       if H.allGeneralsRevealed(target) and room:askToChoice(player, {
           choices = {"wk_heg__yujian_hide::" .. target.id, "Cancel"},
           skill_name = yujian.name
       }) ~= "Cancel" then
        for _, p in ipairs({target}) do
           local isDeputy = H.doHideGeneral(room, player, p, yujian.name)
           room:setPlayerMark(p, "@wk_heg__yujian_reveal-turn", H.getActualGeneral(p, isDeputy))
           local record = type(p:getMark(MarkEnum.RevealProhibited .. "-turn")) == "table" and p:getMark(MarkEnum.RevealProhibited .. "-turn") or {}
           table.insert(record, isDeputy and "d" or "m")
           room:setPlayerMark(p, MarkEnum.RevealProhibited .. "-turn", record)
        end
      end
    end,
})

yujian:addEffect(fk.TurnEnd,{
    is_delay_effect = true,
    anim_type = "special",
    can_trigger = function (self, event, target, player, data)
       return player:getMark("@@wk_heg__yujian_exchange-turn") > 0 and not player.dead and not player.room.current.dead
    end,
    on_cost = Util.TrueFunc,
    on_use = function (self, event, target, player, data)
       local room = player.room
       local current = room.current
        room:swapAllCards(player, {player, current}, yujian.name, "h")
    end,
})

return yujian